import subprocess
import sys
import time
from sys import argv

from pykeyboard import PyKeyboard
from pymouse import PyMouse


def main():
    if len(argv) < 2:
        print("argv's len is less 1.", argv)
        sys.exit("args error")
    mouse = PyMouse()
    try:
        x = int(argv[1])
        y = int(argv[2])
        print(x, y)
        mouse.move(x, y)
        mouse.click(x, y, 1)
        if argv[3] is None:
            mouse.scroll(vertical=argv[3])  # https://github.com/SavinaRoja/PyUserInput/blob/master/pymouse/mac.py#L57
    except Exception as e:
        print(e)


def doc88_web_screencapture():
    mouse = PyMouse()
    for i in range(42):
        page = i + 1
        print(page)
        file_name = "/Users/ddy/Desktop/991/" + str(page) + ".png"
        try:
            x = 1389  # 1389：下一页 1272：上一页
            y = 155
            cmd = "screencapture -R 695,174,940,1266 " + file_name  # 截图起始坐标（左上角坐标：【0，0】）：【695，174】 截图尺寸：940*1266
            subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            time.sleep(1.5)
            # doc88 必须先滚动一下页面才能点击下一页
            mouse.move(x, y + 30)
            time.sleep(0.5)
            mouse.scroll(vertical=-5)
            time.sleep(0.5)
            mouse.move(x, y)
            time.sleep(0.5)
            mouse.click(x, y, 1)
            time.sleep(1)
        except Exception as e:
            print(e)


def baidudoc():
    mouse = PyMouse()
    keyboard = PyKeyboard()
    for i in range(10):
        page = (i+2)
        print(page)
        file_name = "/Users/ddy/Desktop/991/" + str(page) + ".png"
        try:
            x = 1146
            y = 1417
            cmd = "screencapture -R 696,180,963,1206 " + file_name  # 截图起始坐标（左上角坐标：【0，0】）：【695，174】 截图尺寸：940*1266
            subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            time.sleep(1.5)

            mouse.click(x, y, 1)
            keyboard.press_key('delete')
            if page > 9:
                if page >= 10:
                    keyboard.press_key('delete')
                for c in str(page):
                    keyboard.type_string(c)
            else:
                keyboard.type_string(str(page))
            keyboard.press_key('Return')
            time.sleep(1)
        except Exception as e:
            print(e)


def book118():
    mouse = PyMouse()
    for i in range(33):
        page = i + 1
        print(page)
        file_name = "/Users/ddy/Desktop/991/" + str(page) + ".png"
        try:
            x = 1400  # 1389：下一页 1272：上一页
            y = 1415
            cmd = "screencapture -R 893,133,804,1122 " + file_name  # 截图起始坐标（左上角坐标：【0，0】）：【695，174】 截图尺寸：940*1266
            subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            time.sleep(1)
            mouse.click(x, y, 1)
            time.sleep(1)
        except Exception as e:
            print(e)


if __name__ == "__main__":
    book118()
